package com.beyondbit.hr.report;

import com.beyondbit.hr.report.service.ReportService;
import com.beyondbit.hr.report.service.impl.DefaultReportService;
import com.beyondbit.hr.report.support.ReportConverter;
import com.beyondbit.hr.report.support.ReportRenderer;
import com.beyondbit.hr.report.support.render.ExcelRender;
import com.beyondbit.hr.report.support.render.HTMLRender;
import com.beyondbit.hr.report.support.render.PDFRender;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan
public class HRReportConfiguration {

    @Bean
    public ReportService reportService(){
        return new DefaultReportService();
    }


    @Bean
    ReportConverter reportConverter(){
        ReportConverter reportConverter = new ReportConverter();
        return reportConverter;
    }


    @Configuration
    public static class ReportMvcConfiguration implements WebMvcConfigurer {
        @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            /* 是否通过请求Url的扩展名来决定media type */
            configurer.favorPathExtension(true)
                    /* 不检查Accept请求头 */
                    .ignoreAcceptHeader(false)
                    .parameterName("mediaType")
                    /* 设置默认的media yype */
                    .defaultContentType(MediaType.APPLICATION_JSON)
                    /* 请求以.html结尾的会被当成MediaType.TEXT_HTML*/
                    .mediaType("html", MediaType.TEXT_HTML)
                    /* 请求以.json结尾的会被当成MediaType.APPLICATION_JSON*/
                    .mediaType("json", MediaType.APPLICATION_JSON)
                    .mediaType("xls", ReportConverter.XLS)
                     .mediaType("pdf", ReportConverter.PDF);
        }
    }


    @Configuration
    public static class ReportConfiguration{

        @Bean
        public ReportRenderer excelRender(){
            return new ExcelRender();
        }

        @Bean
        public ReportRenderer pdfRender(){
            return new PDFRender();
        }

        @Bean
        public ReportRenderer htmlRender(){
            return new HTMLRender();
        }
    }

}
